define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function(){
	    this.isInSearch = false;
		this.callParent();
		
		
	};
	Model.prototype.getLeftImageUrl = function(url) {
		return url?this.getParentModel().serverUrl+url:require.toUrl("$UI/CommunityOto/category/img/loading.jpg");
	};

	Model.prototype.nearShopDataCustomRefresh = function(event){
	        console.log('offset',event.offset);
	    	var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/roundBusiness?callback=?";
			var id = this.getParentModel().getCompanyId();
			var page = event.offset/event.limit + 1;
			if(event.source.count() < event.offset){
				return ;
			}
			
	        $.getJSON(url,
	        		{
	        			
					    "company.id":id,
					    "pageSize":event.limit,
					    "pageNo":page
	        			
	        		},
	        		function(data){
	        		    console.log(data);
	        		    if(data && data.data){
	        		       
	        		    	var shopInfoList = new Array();
		        		    for(var i = 0;i < data.data.length;i++){
		        		        data.data[i].userBusiness.name =  data.data[i].name;
		        		        data.data[i].userBusiness.phone = data.data[i].phone;
		        		        data.data[i].userBusiness.photo = data.data[i].photo;
		        		   
		        		    	shopInfoList[i] = data.data[i].userBusiness;
		        		    }   
		        		    event.source.loadData(shopInfoList,true,null,event.offset,true);
		        		    
	        		    }       		    
	        		}
	        ); 
	};
	
//	Model.prototype.nearShopDataCustomRefresh = function(event) {
//	var id = this.getParentModel().getCompanyId();
//
//
//        var me = this;
//        
//		$.ajax({
//			url : "http://www.xjzlhy.com:81/ex_community/a/api/community/roundBusiness?callback=?",
//			async : false,
//			cache : false,
//			data : {
//				"pageSize":me.pSize,
//				"pageNo":me.pNo,
//				"company.id":id
//			},
//			dataType : "jsonp",
//			jsonp : "callback",
//			success : function(data) {
//			 if(data && data.data){
//			           console.log(data.data);
//			           console.log(me.pageSize + me.pageNo);
//        		    	var shopInfoList = new Array();
//	        		    for(var i = 0;i < data.data.length;i++){
//	        		        data.data[i].userBusiness.name =  data.data[i].name;
//	        		        data.data[i].userBusiness.phone = data.data[i].phone;
//	        		        data.data[i].userBusiness.photo = data.data[i].photo;
//	        		    	shopInfoList[i] = data.data[i].userBusiness;
//	        		    }   
//	        		    event.source.loadData(shopInfoList);
//	        		    
//        		    }    
//				
//				
////				popOverLoading.hide();
//			},
//			error : function() {
////			popOverLoading.hide();
//				throw justep.Error.create("加载数据失败");
//			}
//		});
//	};
//	
	

	Model.prototype.backBtnClick = function(event){
	
	    if(this.isInSearch){
	    	this.comp("nearShopData").refreshData();
	        this.isInSearch = false;
	        this.comp("searchContent").val('');
	    }else{
	    	this.comp("windowReceiver1").windowEnsure();
	    	this.getParentModel().goBackHome();
	    }
	    

	};

	Model.prototype.showSearchButtonClick = function(event){
	
		$('.search-panel').slideToggle();

	};

	Model.prototype.searchButtonClick = function(event){
	
	    var searchUrl = 'http://www.xjzlhy.com:81/ex_community/a/api/community/roundBusiness?callback=?'
	    var searchName = this.comp("searchContent").val();
	    if(searchName){
	    	var companyId = this.getParentModel().getCompanyId();
		    var me = this;
		     $.getJSON(searchUrl,{'company.id':companyId,'name':searchName},function(data){
		    	  me.comp("nearShopData").clear();
		    	  if(data && data.data){
		    	        me.isInSearch = true;
	    		    	var shopInfoList = new Array();
	        		    for(var i = 0;i < data.data.length;i++){
	        		        data.data[i].userBusiness.name =  data.data[i].name;
	        		    	shopInfoList[i] = data.data[i].userBusiness;
	        		    }   
	        		    me.comp("nearShopData").loadData(shopInfoList,true);
	    		    }     
		     
		     });
	    
	    }

	};

    //进入商家详情页
	
	Model.prototype.shopClick = function(event){
	
		var currentRow = this.comp("nearShopData").getCurrentRow();
		var option = {
				"name":currentRow.val("name"),
				"phone":currentRow.val('phone'),
				"images":currentRow.val("images"),
				"introduce":currentRow.val("introduce"),
				"price":currentRow.val("price")
		};
		justep.Shell.showPage("$UI/communityOto/home/shopIntroduce.w",option);

	};
	Model.prototype.scrollView1PullDown = function(event){
	   this.comp("nearShopData").refreshData();
	};


	Model.prototype.scrollView1PullUp = function(event){
		this.comp("nearShopData").loadNextPageData();

	};
	


	return Model;
});